import 'dart:async';

import 'package:app_tracking_transparency/app_tracking_transparency.dart';
import 'package:flutter/cupertino.dart';

class LYAppTrackingTransparency {
  LYAppTrackingTransparency._internal();
  static final LYAppTrackingTransparency _instance =
      LYAppTrackingTransparency._internal();
  static LYAppTrackingTransparency get share {
    return _instance;
  }

  Timer? _timer;
  void init() {
    _timer = Timer.periodic(const Duration(seconds: 2), (timer) {
      // 在这里执行你想要的任务
      requestTrackingTransparency();
    });
  }

  void requestTrackingTransparency() async {
    final TrackingStatus status =
        await AppTrackingTransparency.trackingAuthorizationStatus;
    if (status == TrackingStatus.notDetermined) {
      Future.delayed(const Duration(seconds: 2), () async {
        await AppTrackingTransparency.requestTrackingAuthorization();
      });
    } else {
      _timer?.cancel();
      _timer = null;
    }
  }
}
